require("../../assets/css/index.less");
require("./mydata.less");
require("../../assets/fonts/iconfont.css");
require("../../assets/fonts_three/iconfont.css");
require("../../utils/dom");
require("weui");
const https = require('../../utils/https')
const weui = require("weui.js");
const axios = require("axios");
require('../../utils/https');
axios.defaults.headers.common['Authorization'] = JSON.parse(localStorage.getItem("token"));


function $(a) {
    return document.querySelector(a);
}

$("#img").src = JSON.parse(localStorage.getItem("user")).imgUrl || "#";
$("#js_current_input").value = JSON.parse(localStorage.getItem("user")).nickName;
$(".weui-textarea").value = JSON.parse(localStorage.getItem("user")).sign;
$(".weui-select").textContent = JSON.parse(localStorage.getItem("user")).gender === 0 ? "男" : "女";
$("#birthday").textContent = JSON.parse(localStorage.getItem("user")).birthday;
async function provincee() {
    const res = await axios.get("/api/shared/province")
    console.log(res.data.data);
    if (res.data.errno === 0) {
        let arr = res.data.data.filter((v, i) => {
            if (v.id == JSON.parse(localStorage.getItem("user")).provinceId) {
                return v.name;
            }
        })
        console.log(arr);
        $("#province").textContent = arr[0].name;
    }
}
provincee();
// $("#city").textContent = JSON.parse(localStorage.getItem("city"));
// console.log(JSON.parse(localStorage.getItem("user")).provinceId);
async function city() {
    const res = await axios.get("/api/shared/city", { "params": { provinceId: JSON.parse(localStorage.getItem("user")).provinceId } })
    console.log(res);
    if (res.data.errno === 0) {
        let arr = res.data.data.filter((v, i) => {
            if (v.id == JSON.parse(localStorage.getItem("user")).cityId) {
                return v.name;
            }
        })
        console.log(arr);
        $("#city").textContent = arr[0].name;
    }
}
city();
$(".span").textContent = $(".weui-textarea").value.length;


$(".icon-zuojiantou").addEventListener("click", function() {
    console.log(1);
    location.href = "/my.html";
})

$("#file").addEventListener("change", function() {
    // console.log(111);
    let f = this.files[0];
    // console.dir(f);
    let fd = new FormData();
    // console.dir(fd);
    fd.append("file", f);
    // console.dir(fd.get("file"));
    async function photograph() {
        const res = await axios.post("/api/shared/uploadPortrait", fd)
        console.log(res.data.data.url);
        console.log(res);
        if (res.data.errno == 0) {
            $("#img").src = https.BASE_URL + res.data.data.url;
        }
    }
    photograph();
})

let gender;
$('#showPicker').addEventListener('click', function() {
    weui.picker([{
        label: '男',
        value: 0
    }, {
        label: '女',
        value: 1
    }], {
        onChange: function(result) {
            console.log(result);
        },
        onConfirm: function(result) {
            console.log(result);
            $(".weui-select").textContent = result[0].label;
            gender = result[0].value;
        },
        title: '性别'
    });
});

document.querySelector('#birthdaybox').addEventListener('click', function() {
    weui.datePicker({
        start: 1990,
        end: new Date().getFullYear(),
        onConfirm: function(result) {
            console.log(result);
            let a = "";
            result.map(function(v) {
                v < 10 ? v = `0${v}` : v = v;
                a += `${v}-`;
                const b = a.slice(0, -1);
                // console.log(b);
                $("#birthday").textContent = b;
            })
        },
        title: '日期'
    });
});

let provinceId;
let cityId;
async function province() {
    const res = await axios.get("api/shared/province")
    if (res.data.errno === 0) {
        const province = res.data.data;
        let arr = province.map(function(v, i) {
            return {
                label: v.name,
                value: v.id,
            }
        });

        // 省份
        $('#provincebox').addEventListener('click', function() {
            weui.picker(arr, {
                onChange: function(result) {
                    console.log(result);
                },
                onConfirm: function(result) {
                    console.log(result);
                    $("#province").textContent = result[0].label;
                    provinceId = result[0].value;
                },
                title: '省份'
            });
        });

        $("#citybox").addEventListener("click", function() {
            if (provinceId) {
                async function city() {
                    const res = await axios.get("/api/shared/city", { "params": { provinceId } })
                    console.log(res.data.errno);
                    if (res.data.errno === 0) {
                        let city = res.data.data.map((v, i) => {
                            return {
                                label: v.name,
                                value: v.id,
                            }
                        })
                        weui.picker(city, {
                            onChange: function(result) {
                                console.log(result);
                            },
                            onConfirm: function(result) {
                                console.log(result);
                                $("#city").textContent = result[0].label;
                                cityId = result[0].value;
                            },
                            title: '城市',
                        });
                    }

                }

                city();
            } else {
                alert('请先选择省份');
            }

        });
    }
}
province();

$(".weui-textarea").addEventListener("input", function() {
    $(".span").textContent = $(".weui-textarea").value.length;
    console.log($(".weui-textarea").value);
});

$("#btn").addEventListener("click", function() {
    async function fn() {
        const res = await axios.post("/api/user/changeInfo", {
            nickName: $("#js_current_input").value || "",
            gender: gender,
            provinceId,
            cityId,
            birthday: $("#birthday").textContent,
            imgUrl: $("#img").src || "#",
            sign: $(".weui-textarea").value || "什么都没留下",
        })
        console.log(res);
        if (res.data.errno === 0) {
            localStorage.setItem("token", JSON.stringify(res.data.data.token));
            location.href = "/my.html";
        } else {
            alert('保存失败')
        }
    };
    fn()
})